/*
 * aks_processor.h
 *
 *  Created on: 2011/05/05
 *      Author: AKS
 */

#ifndef AKS_PROCESSOR_H_
#define AKS_PROCESSOR_H_

#include "types.h"
#include "chip/include/chip.h"
/*
 * The abstracted CPU is required.
 *
 * External SRAM Access :
 *     24-bit address space
 *     8-bit  data width
 *
 * UART Ports :
 *     1. for Debugging
 *     2. for Sub-system
 *
 * I2C Port   :
 *     1. for Sub-system
 *
 * SPI Port   :
 *     1. for Sub-system
 *
 * GPIO       :
 *     [2..0]. for Sub-system Address
 *     [7..3]. for Sub-system GPIO
 *     [8]   . for Sub-system Reset
 * EXT INT    :
 *     1. for Sub-system interrupt
 *
 * Timer :
 *     [0]   . free run tick
 *     [8..1]. 0-stop tick
 */

/* types for processor */
typedef enum t_UART_ID {UART_DBG, UART_SUB_SYSTEM} UART_ID;
typedef void (*UART_RCV_CALLBACK)(BYTE); ///

typedef enum t_GPIO_ID  {GPIO_0, GPIO_1, GPIO_2, GPIO_3, GPIO_4} GPIO_ID;
typedef enum t_GPIO_DIR {GPIO_IN, GPIO_OUT} GPIO_DIR;

typedef void (*TIMER_CALLBACK)(void);
typedef enum t_TIMER_ID {TIMER_0, TIMER_1, TIMER_2, TIMER_3,
                         TIMER_4, TIMER_5, TIMER_6, TIMER_7, TIMER_SYS, TIMER_SYS2, TIMER_NUM } TIMER_ID;

/* abstracted CPU functions */
BOOL Init_processor(BYTE clkMHz);
BYTE getSystemClockMHz(void);

/* Interrupt controller */
#define Enable_Int()      _Enable_Int()
#define Disable_Int()     _Disable_Int()

/* External SRAM system function/macro*/
#define EXTBUS_read(addr)            _EXBUS_read(addr)
#define EXTBUS_write(addr, data)     _EXBUS_write(addr, data)


/* UART system function */
BOOL Init_UART(UART_ID portID, WORD baudrate);
BOOL UART_setReceiveCallback(UART_ID portID, UART_RCV_CALLBACK callback);
BOOL UART_transmitByte(UART_ID portID, BYTE buf);
BOOL UART_transmitBytes(UART_ID portID, BYTE *buf, BYTE len);
BOOL UART_transmitString(UART_ID portID, BYTE *buf);

/* I2C system function */
BOOL Init_I2C(BYTE ownAddress, WORD clk_KHz);
BOOL I2C_transmitBytes(BYTE slaveAdr, BYTE *tx_buf, BYTE tx_len);
BOOL I2C_receiveBytes (BYTE slaveAdr, BYTE *tx_buf, BYTE tx_len, BYTE *rx_buf, BYTE rx_len);

/* SPI system function */
BOOL Init_SPI(WORD clk_KHz);
BOOL SPI_transmitBytes(BYTE *tx_buf, BYTE tx_len);
BOOL SPI_receiveBytes (BYTE *tx_buf, BYTE tx_len, BYTE *rx_buf, BYTE rx_len);

/* Timer system function */
BOOL Init_Timer(BYTE tick_ms);
BOOL TIMER_resetCallback(TIMER_ID id, TIMER_CALLBACK count);
WORD TIMER_systemTick(void);

/* GPIO system function */
BOOL Init_GPIO(void);

#define _TGT_current()	(PORTA.IN & 0x7)
#define _TGT0_Lo()      (PORTA.OUTCLR = 0x01)
#define _TGT1_Lo()      (PORTA.OUTCLR = 0x02)
#define _TGT2_Lo()      (PORTA.OUTCLR = 0x04)

#define _TGT0_Hi()      (PORTA.OUTSET = 0x01)
#define _TGT1_Hi()      (PORTA.OUTSET = 0x02)
#define _TGT2_Hi()      (PORTA.OUTSET = 0x04)


#define GIO0_state()    _GIO0_state()
#define GIO1_state()    _GIO1_state()
#define GIO2_state()    _GIO2_state()
#define GIO3_state()    _GIO3_state()
#define GIO_OR_state    _GIO_OR_state()

#define GIO0_Lo()       _GIO0_Lo()
#define GIO1_Lo()       _GIO1_Lo()
#define GIO2_Lo()       _GIO2_Lo()
#define GIO3_Lo()       _GIO3_Lo()
#define GIO_OR_Lo()     _GIO_OR_Lo()

#define GIO0_Hi()       _GIO0_Hi()
#define GIO1_Hi()       _GIO1_Hi()
#define GIO2_Hi()       _GIO2_Hi()
#define GIO3_Hi()       _GIO3_Hi()
#define GIO_OR_Hi()     _GIO_OR_Hi()

#define GIO0_dir_O()    _GIO0_dir_O()
#define GIO1_dir_O()    _GIO1_dir_O()
#define GIO2_dir_O()    _GIO2_dir_O()
#define GIO3_dir_O()    _GIO3_dir_O()
#define GIO_OR_dir_O()  _GIO_OR_dir_O()

#define GIO0_dir_I()    _GIO0_dir_I()
#define GIO1_dir_I()    _GIO1_dir_I()
#define GIO2_dir_I()    _GIO2_dir_I()
#define GIO3_dir_I()    _GIO3_dir_I()
#define GIO_OR_dir_I()  _GIO_OR_dir_I()

/* Sub system function */
BOOL Init_SubSystem(void);
void SUB_SYSTEM_reset(void);
BOOL SUB_SYSTEM_select(BYTE ssAdr);
BYTE SUB_SYSTEM_interruptFlag(void);

#define SS_RESET_ASSERT()	_SS_RESET_ASSERT()
#define SS_RESET_NEGATE()	_SS_RESET_NEGATE()

#endif /* AKS_PROCESSOR_H_ */
